<?php

namespace App\Http\Controllers;

use App\Models\MaintainOrder;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class MaintainOrderController extends Controller
{
    public function index()
    {
        if (empty(Auth::user())){
            return redirect()->route('login');
        }
        $maintainOrder = MaintainOrder::query()
            ->where(function ($query){
                $query->where('apply_user',Auth::id())->OrWhere('maintain_user',Auth::id());
            })
            ->orderBy('id','desc')
            ->get();
        return view('home/maintain-order', ['maintainOrder' => $maintainOrder,'is_maintain_user' => User::isMaintainUser(Auth::id())]);
    }

    public function show($id)
    {
        $maintainOrder = MaintainOrder::query()->find($id);
        $maintainOrder->files = explode('|', $maintainOrder->apply_file);
        $maintainOrder->maintain_images = explode('|', $maintainOrder->maintain_images);
        return view('home/maintain-order-details', ['data' => $maintainOrder,'is_maintain_user' => User::isMaintainUser(Auth::id())]);
    }

    public function add()
    {
        if (request()->isMethod('post')){
            $data = \request()->all();
            if (empty($data['file'])){
                return ['code' => 1, 'msg' => '图片必传'];
            }
            $maintainOrder = new MaintainOrder();
            $maintainOrder->order_name      = $data['order_name'];
            $maintainOrder->maintain_site   = $data['maintain_site'];
            $maintainOrder->apply_content   = $data['apply_content'];
            $maintainOrder->apply_file      = trim($data['file'],'|');
            $maintainOrder->apply_user      = Auth::id();
            $res = $maintainOrder->save();
            if (!$res){
                return ['code' => 1, 'msg' => '提交失败'];
            }
            return ['code' => 0, 'msg'=> '提交成功'];
        }
        return view('home/maintain-order-add');
    }

    public function edit($id)
    {
        $maintainOrder = MaintainOrder::query()->find($id);
        if (request()->isMethod('post')){
            $data = \request()->all();
            $maintainOrder->is_replace_maintain     = $data['is_replace_maintain'];
            $maintainOrder->maintain_money          = $data['maintain_money'];
            $maintainOrder->mounting_money          = $data['mounting_money'];
            $maintainOrder->sum_money               = $data['maintain_money'] + $data['mounting_money'];
            $maintainOrder->maintain_time           = date('Y-m-d H:i:s');
            $maintainOrder->maintain_images         = trim($data['file'],'|');
            $maintainOrder->order_status            = 2;
            $res = $maintainOrder->save();
            if (!$res){
                return ['code' => 1, 'msg' => '提交失败'];
            }
            return ['code' => 0, 'msg'=> '提交成功'];
        }
        return view('home/maintain-order-edit', ['data' => $maintainOrder]);
    }



}
